#!/bin/bash

echo Setup a clean redist directory
rm -rf redist
mkdir -p redist
cp LICENSE.GPL redist/
cp README.txt redist/
cp -RP nobitter.app redist/
mkdir -p redist/nobitter.app/Contents/Frameworks

echo Copying Qt Frameworks to nobitter bundle...
pushd redist/nobitter.app/Contents/Frameworks > /dev/null
cp -RP /Library/Frameworks/QtCore.framework ./
cp -RP /Library/Frameworks/QtGui.framework ./
cp -RP /Library/Frameworks/QtXml.framework ./
cp -RP /Library/Frameworks/QtNetwork.framework ./
popd > /dev/null

pushd redist/nobitter.app/Contents > /dev/null
mkdir -p Plugins
cp -RP /Developer/Applications/Qt/plugins/imageformats/libqjpeg.dylib Plugins/libqjpeg.dylib
cp -RP /Developer/Applications/Qt/plugins/imageformats/libqtiff.dylib Plugins/libqtiff.dylib
cp -RP /Developer/Applications/Qt/plugins/imageformats/libqgif.dylib Plugins/libqgif.dylib
popd > /dev/null

# Remove unecessary Qt binaries, headers etc.
echo Stripping Qt trash from nobitter.app
pushd redist/nobitter.app/Contents/Frameworks > /dev/null
rm -rf QtGui.framework/QtGui_debug
rm -rf QtGui.framework/Versions/4/QtGui_debug
rm -rf QtGui.framework/Versions/Current/QtGui_debug
rm -rf QtGui.framework/Headers
rm -rf QtGui.framework/Versions/4/Headers
rm -rf QtGui.framework/Versions/Current/Headers
rm -rf QtCore.framework/Headers
rm -rf QtCore.framework/Versions/4/Headers
rm -rf QtCore.framework/Versions/Current/Headers
rm -rf QtCore.framework/QtCore_debug
rm -rf QtCore.framework/Versions/4/QtCore_debug
rm -rf QtCore.framework/Versions/Current/QtCore_debug
rm -rf QtXml.framework/QtXml_debug
rm -rf QtXml.framework/Versions/4/QtXml_debug
rm -rf QtXml.framework/Versions/Current/QtXml_debug
rm -rf QtXml.framework/Headers
rm -rf QtXml.framework/Versions/4/Headers
rm -rf QtXml.framework/Versions/Current/Headers
rm -rf QtNetwork.framework/QtNetwork_debug
rm -rf QtNetwork.framework/Versions/4/QtNetwork_debug
rm -rf QtNetwork.framework/Versions/Current/QtNetwork_debug
rm -rf QtNetwork.framework/Headers
rm -rf QtNetwork.framework/Versions/4/Headers
rm -rf QtNetwork.framework/Versions/Current/Headers
popd > /dev/null

# Set the framework locations, so the bundle will run on a clean MAC
echo Setting Frameworks...
install_name_tool -change /Library/Frameworks/QtGui.framework/Versions/4/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/Current/QtGui redist/nobitter.app/Contents/MacOS/nobitter
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/MacOS/nobitter
install_name_tool -change /Library/Frameworks/QtNetwork.framework/Versions/4/QtNetwork @executable_path/../Frameworks/QtNetwork.framework/Versions/Current/QtNetwork redist/nobitter.app/Contents/MacOS/nobitter
install_name_tool -change /Library/Frameworks/QtXml.framework/Versions/4/QtXml @executable_path/../Frameworks/QtXml.framework/Versions/Current/QtXml redist/nobitter.app/Contents/MacOS/nobitter

# Qt libs in nobitter.app
echo on QtGui
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/Frameworks/QtGui.framework/Versions/Current/QtGui
echo on QtXml
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/Frameworks/QtXml.framework/Versions/Current/QtXml
install_name_tool -change /Library/Frameworks/QtGui.framework/Versions/4/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/Current/QtGui redist/nobitter.app/Contents/Frameworks/QtXml.framework/Versions/Current/QtXml
install_name_tool -change /Library/Frameworks/QtSvg.framework/Versions/4/QtSvg @executable_path/../Frameworks/QtSvg.framework/Versions/Current/QtSvg redist/nobitter.app/Contents/Frameworks/QtXml.framework/Versions/Current/QtXml
install_name_tool -change /Library/Frameworks/QtXml.framework/Versions/4/QtXml @executable_path/../Frameworks/QtXml.framework/Versions/Current/QtXml redist/nobitter.app/Contents/Frameworks/QtXml.framework/Versions/Current/QtXml
echo on QtNetwork
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/Frameworks/QtNetwork.framework/Versions/Current/QtNetwork
echo on libqjpeg
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/Plugins/libqjpeg.dylib
install_name_tool -change /Library/Frameworks/QtGui.framework/Versions/4/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/Current/QtGui redist/nobitter.app/Contents/Plugins/libqjpeg.dylib
# handle image plugins in nobitter.app
echo on libqtiff
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/Plugins/libqtiff.dylib
install_name_tool -change /Library/Frameworks/QtGui.framework/Versions/4/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/Current/QtGui redist/nobitter.app/Contents/Plugins/libqtiff.dylib
echo on libqjgif
install_name_tool -change /Library/Frameworks/QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/Current/QtCore redist/nobitter.app/Contents/Plugins/libqgif.dylib
install_name_tool -change /Library/Frameworks/QtGui.framework/Versions/4/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/Current/QtGui redist/nobitter.app/Contents/Plugins/libqgif.dylib

echo Finished!
